package com.design.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @auther LiZiBa
 * @date 2020/8/8 11:28
 * @description: 容器式单例类似spring IOC
 **/
public class ContainerSingleton {

    private ContainerSingleton() {
    }

    private static Map<String, Object> ioc = new ConcurrentHashMap<String, Object>();

    public static Object getInstance(String className){

        if(!ioc.containsKey(className)){
            Object o = null;
            try{
              o = Class.forName(className).newInstance();
              ioc.put(className, o);
            }catch (Exception e){
                e.printStackTrace();
            }
            return o;
        } else{
            return ioc.get(className);
        }

    }
}
